package it.localhost.deepthought.one.fragment;

import it.localhost.deepthought.one.R;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.WindowManager;

public class ProgressDialogFragment extends DialogFragment {
	private static final String KEY_MSG = "KEY_MSG";
	private static final String KEY_MAX = "KEY_MAX";
	private static final String KEY_STYLE = "KEY_STYLE";
	private ProgressDialog pd;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		pd = new ProgressDialog(getActivity());
		pd.setTitle(getArguments().getInt(KEY_MSG));
		setCancelable(false);
		pd.setCancelable(false);
		pd.setCanceledOnTouchOutside(false);
		pd.setProgressStyle(getArguments().getInt(KEY_STYLE));
		if (getArguments().getInt(KEY_STYLE) == ProgressDialog.STYLE_SPINNER)
			pd.setMessage(getString(R.string.wait));
		else
			pd.setMax(getArguments().getInt(KEY_MAX));
		return pd;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	public void setProgress(int value) {
		if (pd != null)
			pd.setProgress(value);
	}

	public void show(Activity context, int resId, int max, int style) {
		Bundle arguments = new Bundle(3);
		arguments.putInt(KEY_MSG, resId);
		arguments.putInt(KEY_MAX, max);
		arguments.putInt(KEY_STYLE, style);
		setArguments(arguments);
		show(context.getFragmentManager(), ProgressDialogFragment.class.getSimpleName());
	}
}